
#include <BListWidget>
#include <BListItem>
#include "member_BListWidget.h"
#include "member_BListItem.h"

using namespace BWE;

#define member					(*(member_BListItem*)_ptr)
#define member_allocate()		_ptr = new member_BListItem(this)
#define member_release()		delete (member_BListItem*)_ptr
#define layout_member(listWidget)	((member_BListWidget*)listWidget->_ptr)

BListItem::BListItem()
{
	member_allocate();
}
BListItem::BListItem(const BString& text)
{
	member_allocate();
	member.text = text;
}
BListItem::BListItem(Graph graph, const BString& text)
{
	member_allocate();
	member.graph = graph;
	member.text = text;
}
BListItem::BListItem(BIcon* icon, const BString& text)
{
	member_allocate();
	member.icon = icon;
	member.text = text;
}
BListItem::BListItem(BImage* image, const BString& text)
{
	member_allocate();
	member.image = image;
	member.text = text;
}
BListItem::BListItem(BWidget* widget, const BString& text)
{
	member_allocate();
	member.widget = widget;
	member.text = text;
}
BListItem::~BListItem()
{
	member_release();
}

void BListItem::setVisible(bool visible)
{
	if (member.visible != visible)
	{
		member.visible = visible;
		member.dirtyCell();
		if (member.selected)
			this->setSelected(false);
	}
}
bool BListItem::visible() const
{
	return member.visible;
}

void BListItem::setAlign(Align align)
{
	if (member.ia_align != align)
	{
		member.ia_align = align;
		member.dirty = true;
		member.dirtyCell();
	}
}
Align BListItem::align() const
{
	return member.ia_align;
}

void BListItem::setForm(Form form)
{
	if (member.ia_form != form)
	{
		member.ia_form = form;
		member.dirty = true;
		member.dirtyCell();
	}
}
Form BListItem::form() const
{
	return member.ia_form;
}

void BListItem::setGraph(Graph graph)
{
	if (member.graph != graph)
	{
		member.graph = graph;
		member.dirty = true;
		member.dirtyCell();
	}
}
Graph BListItem::graph() const
{
	return member.graph;
}

void BListItem::setIcon(BIcon* icon)
{
	if (member.icon != icon)
	{
		member.icon = icon;
		member.dirtyCell();
	}
}
BIcon* BListItem::icon() const
{
	return member.icon;
}

void BListItem::setIconSize(int iconWidth, int iconHeight)
{
	setIconSize(BSize(iconWidth, iconHeight));
}
void BListItem::setIconSize(const BSize& iconSize)
{
	if (member.iconSize != iconSize)
	{
		member.iconSize = iconSize;
		member.dirty = true;
		member.dirtyCell();
	}
}
const BSize& BListItem::iconSize() const
{
	return member.iconSize;
}

void BListItem::setText(const BString& text)
{
	if (member.text != text)
	{
		member.text = text;
		member.dirtyCell();
		emit(Signal_TextChanged, member.text);
	}
}
const BString& BListItem::text() const
{
	return member.text;
}

void BListItem::setImage(BImage* image)
{
	if (member.image != image)
	{
		member.image = image;
		member.dirtyCell();
		emit(Signal_ImageChanged, member.image);
	}
}
BImage* BListItem::image()
{
	return member.image;
}
const BImage* BListItem::image() const
{
	return member.image;
}

void BListItem::setColor(BByte r, BByte g, BByte b, BByte a)
{
	setColor(BColor(r, g, b, a));
}
void BListItem::setColor(const BColor& color)
{
	if (member.color != color)
	{
		member.color = color;
		if (member.list)
			member.list->fresh();
	}
}
const BColor& BListItem::color() const
{
	return member.color;
}

void BListItem::setWidget(BWidget* widget)
{
	if (member.widget != widget)
	{
		if (member.list && member.widget)
		{
			member.list->removeChild(member.widget);
		}
		member.widget = widget;
		if (member.list && member.widget)
		{
			member.list->addChild(member.widget);
			member.dirtyCell();
		}
	}
}
BWidget* BListItem::widget() const
{
	return member.widget;
}

void BListItem::setSelected(bool selected)
{
	if (member.list && member.selected != selected)
	{
		member.list->setItemSelected(this, selected);
	}
}
bool BListItem::isSelected() const
{
	return member.list && member.selected;
}

int BListItem::index() const
{
	if (member.list)
		return member.index;
	return -1;
}
BRect BListItem::rect() const
{
	if (member.list)
		return member.rect + member.list->area().pos();
	return member.rect;
}
BRect BListItem::iconRect() const
{
	if (member.list)
		return member.ia_imageRect + member.list->area().pos();
	return member.ia_imageRect;
}
BRect BListItem::textRect() const
{
	if (member.list)
		return member.ia_textRect + member.list->area().pos();
	return member.ia_textRect;
}
BRect BListItem::textArea() const
{
	if (member.list)
		return member.ia_textArea + member.list->area().pos();
	return member.ia_textArea;
}

BListWidget* BListItem::listWidget()
{
	return member.list;
}
const BListWidget* BListItem::listWidget() const
{
	return member.list;
}
